var md5 = require('blueimp-md5');
var _ = require('underscore');

var APPEND_WORD = 'youa@yuns'

var userService = {}

userService.loginMap = {}

userService.getUserCsrfToken = function (username) {
  if (username) {
    var csrtToken = md5(username + APPEND_WORD + Date.now());
    return csrtToken;
  }
}

userService.login = function (username) {
  if (_.has(userService.loginMap, username)) {
    return userService.loginMap[username]
  }else{
    var csrtToken = userService.getUserCsrfToken(username)
    userService.loginMap[username] = csrtToken
    return csrtToken
  }
}

userService.checkUserLogin = function (username, csrfToken) {
  return userService.loginMap[username] == csrfToken
}

userService.logout = function (username) {
  if (_.has(userService.loginMap, username)) {
    userService.loginMap = _.omit(userService.loginMap, username);
  }

  return true;
}

module.exports = userService;